/**
 * Created by jiangqifan on 16/3/1.
 */

require('../models/member.js');
var mongoose = require('mongoose');
var Member = mongoose.model('Member');
//var utility = require('utility');
//var uuid    = require('node-uuid');


/**
 * 获取某项目所有成员及其角色信息
 * @param projectId
 * @param callback
 */

exports.getMembers = function (projectId, callback) {
    Member.find({'projectId': projectId}, callback);
};


/**
 * 添加成员
 * @param body
 * @param callback
 */
exports.addMember = function (body, callback) {
    var memberInfo = {
        'projectId': body.projectId,
        'projectTitle': body.projectTitle,
        'user': body.user,
        'role': body.role
    };
    var member = new Member(memberInfo);
    member.save(callback);
};


exports.removeMember = function (memberId, callback) {
    Member.findOneAndRemove({_id: {$in: memberId}}, function(err,doc){
        if(err){
            callback(err,null)
        }else{
            callback(null,doc)
        }
    });
};


exports.updateMember = function (member_id, role, callback) {
    Member.update({_id: member_id}, {role: role}, callback);
};


/**
 * 获取某用户在某项目中的角色
 * @param projectId
 * @param user
 * @param callback
 */
exports.getRole = function (projectId, user, callback) {
    Member.findOne({'projectId': projectId, 'user': user}, callback);
};

/**
 * 获取某用户创建的|参与的|所有相关的项目
 * @param user
 * @param role
 * @param callback
 */
exports.getProjects = function (user, role, callback) {
    switch (role) {
        case 'owner':
            Member.find({'user': user, 'role': 'owner'}, callback);
            break;
        case 'member':
            Member.find({
                'user': user, 'role': {
                    $in: [
                        "admin",
                        "developer",
                        "observer"]
                }
            }, callback);
            break;
        case 'all':
            Member.find({'user': user}, callback);
            break;
        default:
            Member.find({'user': user}, callback);
    }

};
